package com.example.androidexample.animation;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.animation.Animation;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.androidexample.R;

public class AnimationPivotParamsProvider implements RadioGroup.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {

    private static final float DEFAULT_PIVOT_VALUE = 0.5f;
    private int pivotXType = Animation.RELATIVE_TO_SELF;
    private int pivotYType = Animation.RELATIVE_TO_SELF;
    private float pivotXValue = DEFAULT_PIVOT_VALUE;
    private float pivotYValue = DEFAULT_PIVOT_VALUE;

    private TextView tvPivotXValue;
    private TextView tvPivotYValue;
    private RadioGroup rbPivotXType;
    private RadioGroup rbPivotYType;
    private SeekBar skbPivotXValue;
    private SeekBar skbPivotYValue;

    public AnimationPivotParamsProvider(Activity activity) {
        resetPivotParams();
        initView(activity);
        setListener();
    }

    public void resetPivotParams() {
        pivotXType = Animation.RELATIVE_TO_SELF;
        pivotYType = Animation.RELATIVE_TO_SELF;
        pivotXValue = DEFAULT_PIVOT_VALUE;
        pivotYValue = DEFAULT_PIVOT_VALUE;
    }

    public int getPivotXType() {
        return pivotXType;
    }

    public int getPivotYType() {
        return pivotYType;
    }

    public float getPivotXValue() {
        return pivotXValue;
    }

    public float getPivotYValue() {
        return pivotYValue;
    }

    private void setListener() {
        rbPivotXType.setOnCheckedChangeListener(this);
        rbPivotYType.setOnCheckedChangeListener(this);

        skbPivotXValue.setOnSeekBarChangeListener(this);
        skbPivotYValue.setOnSeekBarChangeListener(this);
    }

    private void initView(Activity activity) {
        rbPivotXType = activity.findViewById(R.id.rb_pivot_x_type);
        rbPivotYType = activity.findViewById(R.id.rb_pivot_y_type);

        skbPivotXValue = activity.findViewById(R.id.skb_pivot_x_value);
        skbPivotYValue = activity.findViewById(R.id.skb_pivot_y_value);

        tvPivotXValue = activity.findViewById(R.id.tv_pivot_x_value);
        tvPivotYValue = activity.findViewById(R.id.tv_pivot_y_value);
    }


    @SuppressLint("NonConstantResourceId")
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.cb_relative_to_self_x: {
                pivotXType = Animation.RELATIVE_TO_SELF;
            }
            break;
            case R.id.cb_relative_to_parent_x: {
                pivotXType = Animation.RELATIVE_TO_PARENT;
            }
            break;
            case R.id.cb_relative_to_self_y: {
                pivotYType = Animation.RELATIVE_TO_SELF;
            }
            break;
            case R.id.cb_relative_to_parent_y: {
                pivotYType = Animation.RELATIVE_TO_PARENT;
            }
            break;
        }
    }

    @SuppressLint({"NonConstantResourceId", "SetTextI18n"})
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.skb_pivot_x_value:{
                pivotXValue = progress / 100.0f;
                tvPivotXValue.setText(pivotXValue+"");
            } break;
            case R.id.skb_pivot_y_value:{
                pivotYValue = progress / 100.0f;
                tvPivotYValue.setText(pivotYValue+"");
            } break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }
}
